/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.own.entity.OwnMedicineReceiptInformation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnMedicineAssignment;
import com.jeesite.modules.own.service.OwnMedicineAssignmentService;

import java.util.Map;

/**
 * 药材赋码信息Controller
 * @author F
 * @version 2019-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownMedicineAssignment")
public class OwnMedicineAssignmentController extends BaseController {

	@Autowired
	private OwnMedicineAssignmentService ownMedicineAssignmentService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnMedicineAssignment get(String id, boolean isNewRecord) {
		return ownMedicineAssignmentService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownMedicineAssignment:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnMedicineAssignment ownMedicineAssignment, Model model) {
		model.addAttribute("ownMedicineAssignment", ownMedicineAssignment);
		return "modules/own/ownMedicineAssignmentList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineAssignment:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnMedicineAssignment> listData(OwnMedicineAssignment ownMedicineAssignment, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineAssignment.setPage(new Page<>(request, response));
		Page<OwnMedicineAssignment> page = ownMedicineAssignmentService.findPage(ownMedicineAssignment);
		return page;
	}

	/**
	 * 查询未出库列表数据
	 */
	@RequiresPermissions("own:ownMedicineAssignment:view")
	@RequestMapping(value = "listDataNormal")
	@ResponseBody
	public Page<OwnMedicineAssignment> listDataNormal(OwnMedicineAssignment ownMedicineAssignment, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineAssignment.setPage(new Page<>(request, response));
		ownMedicineAssignment.setOutStatus("0");
		Page<OwnMedicineAssignment> page = ownMedicineAssignmentService.findPage(ownMedicineAssignment);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownMedicineAssignment:view")
	@RequestMapping(value = "form")
	public String form(OwnMedicineAssignment ownMedicineAssignment, Model model) {
		model.addAttribute("ownMedicineAssignment", ownMedicineAssignment);
		return "modules/own/ownMedicineAssignmentForm";
	}

	/**
	 * 保存药材赋码信息
	 */
	@RequiresPermissions("own:ownMedicineAssignment:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnMedicineAssignment ownMedicineAssignment) {
		ownMedicineAssignmentService.save(ownMedicineAssignment);

		return renderResult(Global.TRUE, text("保存药材赋码信息成功！"));
	}

	/**
	 * 删除药材赋码信息
	 */
	@RequiresPermissions("own:ownMedicineAssignment:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnMedicineAssignment ownMedicineAssignment) {
		ownMedicineAssignmentService.delete(ownMedicineAssignment);
		return renderResult(Global.TRUE, text("删除药材赋码信息成功！"));
	}
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineAssignment:view")
	@RequestMapping(value = "findByKey")
	public String plantInfoSelect(OwnMedicineAssignment ownMedicinePlantInformation, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (JsonMapper.fromJson(selectDataJson, Map.class) != null){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("assignmentIformation", ownMedicinePlantInformation);
		return "modules/own/ownMedicineAssignmentSelect";
	}

}